from fastapi import APIRouter,Form,Depends
from fastapi.security import OAuth2PasswordRequestForm
from ..redisbase.redis_operations import get_key
from ..utils.jwt_handler import create_token

admin=APIRouter(prefix="/admin", tags=["管理员"])

USERNAME="admin"
PASSWORD="admin"


@admin.post("/login")
async def loginIn(form_data: OAuth2PasswordRequestForm = Depends(),
                  captcha_id: str = Form,
                  captcha: str = Form):
    username = form_data.username
    password = form_data.password

    # _captcha = get_key(captcha_id)

    # if _captcha != captcha:
    #     return {
    #         "code": 411,
    #         "message": "验证码错误",
    #         "data": None,
    #         # "originUrl": str(request.url)
    #     }
    if username != USERNAME or password != PASSWORD:
        return {
            "code": 401,
            "message": "用户名或密码错误",
            "data": None,
            # "originUrl": str(request.url)
        }
    else:
        user_token = create_token({"username": username})
        return {
            "code": 200,
            "message": "OK",
            "access_token": user_token,
            "token_type": "bearer"
            # "originUrl": str(request.url)
        }